<?php

require_once 'common/conn.php';
$data = array();

$db = new DBC();
$sql = "SELECT task,description FROM t_task_info WHERE type = 1";
$task_array = $db->fetchData($sql);
if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];

    $row = array();
    $row['task'] = "REGISTER";
    $row['description'] = "";
    $row['done'] = "0";
    $row['accept'] = "0";
    $row['id'] = "";
    $sql = "SELECT * FROM t_task_record WHERE usercode = '" . $usercode . "' AND task = 'REGISTER'";
    $accept = $db->fetchData($sql);
    if ($accept != false && count($accept) > 0) {
        $row['id'] = $accept[0]['id'];
        $row['done'] = "1";        
        if ($accept[0]['acceptTime'] != null && $accept[0]['acceptTime'] != "" && $accept[0]['acceptTime'] != "null") {
            $row['accept'] = "1";
        }
    }
    array_push($data, $row);

    $row['task'] = "PINGJIA";
    $row['description'] = "";
    $row['done'] = "0";
    $row['accept'] = "0";
    $row['id'] = "";
    $sql = "SELECT * FROM t_task_record WHERE usercode = '" . $usercode . "' AND task = 'PINGJIA'";
    $done = $db->fetchData($sql);
    if ($done != false && count($done) > 0) {
        $row['id'] = $done[0]['id'];
        $row['done'] = "1";        
        if ($done[0]['acceptTime'] != null && $done[0]['acceptTime'] != "" && $done[0]['acceptTime'] != "null") {
            $row['accept'] = "1";
        }
    }
    array_push($data, $row);

    $sql = "SELECT * FROM t_task_record WHERE usercode = '" . $usercode . "' AND TO_DAYS(createTime) = TO_DAYS(now())";
    $temp = $db->fetchData($sql);
    if ($task_array != false) {
        foreach ($task_array as $row) {
            $row['done'] = "0";
            $row['accept'] = "0";
            if ($temp != false) {
                foreach ($temp as $value) {
                    if ($row['task'] == $value['task']) {
                        $row['id'] = $value['id'];
                        $row['done'] = "1";
                        if ($value['acceptTime'] != null && $value['acceptTime'] != "" && $value['acceptTime'] != "null") {
                            $row['accept'] = "1";
                        }
                        break;
                    }
                }
            }
            if (!isset($row['id'])) {
                $row['id'] = "";
            }
            array_push($data, $row);
        }
    }
}
echo json_encode($data);
?>